package mapping;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import pojo.MyBlog;
import pojo.MyUser;

public interface BlogMapper {

	@Insert("insert into `blog`(`userId`,`title`,`content`) values(#{userId},#{title},#{content})")
	@Options(keyProperty="id",useGeneratedKeys=true)
	void insertBlog(MyBlog b);
	
	@Update("update `blog` set `userId` = #{userId}, `title` = #{title}, `content` = #{content} where `id` = #{id}")
	void updateBlog(MyBlog b);
	
	@Delete("delete from `blog` where id = #{id}")
	void deleteBlog(int id);
	
	@Select("select * from `blog`")
	List<MyBlog> findAllBlogs();
	
	@Select("select * from `blog` where id = #{id}")
	MyBlog findBlogById(int id);
	
	@Select("select * from `blog` where userId = #{userId}")
	MyBlog findBlogByUserId(int id);
	
	@Select("select * from `blog` where id = #{id}")
    @Results(value={
    		@Result(property="id", column="id"),
    		@Result(property="userId", column="userId"),
    		@Result(property="title", column="title"),
    		@Result(property="content", column="content"),
    		@Result(property="author", column="userId", javaType=MyUser.class,
    		        one=@One(select="mapping.userMapper.selectById"))
    })
	MyBlog findBlogById2(int id);
	
	@Select("select * from `user` where id = #{id}")
    @Results(value={
    		@Result(property="id", column="id"),
    		@Result(property="name", column="name"),
    		@Result(property="blogs", column="id", javaType=List.class,
    		        many=@Many(select="mapping.BlogMapper.findBlogByUserId"))
    })
	MyUser findUserById3(int id);

	
}

